﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// ProgressRing.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressRing : UserControl
    {
        public ProgressRing()
        {
            InitializeComponent();
            Loaded += (s, e) => UpdateVisualState();
        }

        public LoadingState State
        {
            get => (LoadingState)GetValue(StateProperty);
            set => SetValue(StateProperty, value);
        }

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register(nameof(State), typeof(LoadingState), typeof(ProgressRing),
                new PropertyMetadata(LoadingState.UnLoading, OnIsLoadingChanged));

        public Brush BaseStroke
        {
            get => (Brush)GetValue(BaseStrokeProperty);
            set => SetValue(BaseStrokeProperty, value);
        }
        public static readonly DependencyProperty BaseStrokeProperty =
            DependencyProperty.Register(nameof(BaseStroke), typeof(Brush), typeof(ProgressRing),
                new PropertyMetadata(new SolidColorBrush(Color.FromRgb(0x44, 0x44, 0x44))));

        public Brush ForeStroke
        {
            get => (Brush)GetValue(ForeStrokeProperty);
            set => SetValue(ForeStrokeProperty, value);
        }
        public static readonly DependencyProperty ForeStrokeProperty =
            DependencyProperty.Register(nameof(ForeStroke), typeof(Brush), typeof(ProgressRing),
                new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#fc0")), OnPropertyChanged));

        private static void OnIsLoadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = (ProgressRing)d;
            ctrl.UpdateVisualState();
        }

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SolidColorBrush newValue = (SolidColorBrush)e.NewValue;
            var progressRing = (d as ProgressRing);
            if (progressRing == null) return;

            Color color = newValue.Color;
            progressRing.st1.Color = color;
            progressRing.st2.Color = progressRing.TransparentConvert(0x30, color);
            progressRing.st3.Color = progressRing.TransparentConvert(0x00, color);
            progressRing.st4.Color = progressRing.TransparentConvert(0x30, color);

        }

        private void UpdateVisualState()
        {
            switch (State)
            {
                case LoadingState.Loading:
                    VisualStateManager.GoToState(this, "Loading", true);
                    break;
                case LoadingState.UnLoading:
                    VisualStateManager.GoToState(this, "NotLoading", true);
                    break;
                default:
                    break;
            }
        }

        private Color TransparentConvert(byte alpha, Color baseColor)
        {
            Color transparentColor = Color.FromArgb(alpha, baseColor.R, baseColor.G, baseColor.B);
            return transparentColor;
        }
    }
}
